﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace SpeedTest
{
	public partial class LoginForm : Form
	{
		public string UserName
		{
			get
			{
				return txtUserName.Text;
			}
		}

		public string Password
		{
			get
			{
				return txtPassword.Text;
			}
		}

		public LoginForm()
		{
			InitializeComponent();
            txtUserName.Focus();
		}

		private void btnLogIn_Click(object sender, EventArgs e)
		{
			if (!String.IsNullOrEmpty(txtPassword.Text) && !String.IsNullOrEmpty(txtUserName.Text))
			{
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			else
			{
				MessageBox.Show("User Name and Password cannot be empty. Please try again.", "Login Error");
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void LoginForm_Load(object sender, EventArgs e)
		{
			chkRememberMe.Checked = Properties.Settings.Default.AutoLogin;

			if (!String.IsNullOrEmpty(Properties.Settings.Default.UserName))
			{
				txtUserName.Text = Properties.Settings.Default.UserName;
			}
			if (!String.IsNullOrEmpty(Properties.Settings.Default.Password))
			{
				txtPassword.Text = Properties.Settings.Default.Password;
			}
		}

		private void linkRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			string errMessage = "To use this shortcut, you must specify the address under Edit->Preferences";
			if (!String.IsNullOrEmpty(Properties.Settings.Default.MainSiteURL))
			{
				try { Process.Start(Properties.Settings.Default.MainSiteURL); }
				catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + errMessage); }
			}
			else
			{
				MessageBox.Show(errMessage);
			}
		}

        private void chkRememberMe_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.AutoLogin = chkRememberMe.Checked;
        }

		private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (chkRememberMe.Checked)
			{
				Properties.Settings.Default.UserName = txtUserName.Text;
				Properties.Settings.Default.Password = txtPassword.Text;
			}

			Properties.Settings.Default.Save();
		}
	}
}
